package com.aquan.map_jetpack.ui.screens


import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Phone
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.aquan.map_jetpack.model.entity.NavigationItem
import com.aquan.map_jetpack.ui.navigation.Destinations

@Composable
fun MainBottomTab() {

    val navController = rememberNavController()

    val navigationItems = listOf(
        NavigationItem(title = "首页", icon = Icons.Filled.Home),
        NavigationItem(title = "新闻", icon = Icons.Filled.Phone),
        NavigationItem(title = "我的", icon = Icons.Filled.Settings)
    )

    val items = listOf(
        Destinations.MapFrame,
        Destinations.NewsFrame,
        Destinations.SettingFrame
    )

    Box(Modifier.navigationBarsPadding()) {
        Scaffold(bottomBar = {

            BottomNavigation() {
                val navBackStackEntry by navController.currentBackStackEntryAsState()
                val currentDestination = navBackStackEntry?.destination
                items.forEachIndexed { index, screen ->
                    BottomNavigationItem(
                        icon = { Icon(navigationItems[index].icon, contentDescription = null) },
                        label = { Text(text = navigationItems[index].title) },
                        selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
                        onClick = {
                            Log.i("=====", navController.graph.findStartDestination().id.toString())


                            navController.navigate(screen.route) {
                                // Pop up to the start destination of the graph to
                                // avoid building up a large stack of destinations
                                // on the back stack as users select items
                                // findStartDestination().
                                navController.graph.startDestinationRoute?.let { route ->
                                    popUpTo(route) {
                                        saveState = true
                                    }
                                }
//                                popUpTo(navController.graph.startDestinationId) {
//                                    saveState = true
//                                }
                                // Avoid multiple copies of the same destination when
                                // reselecting the same item
                                launchSingleTop = true
                                // Restore state when reselecting a previously selected item
                                restoreState = true
                            }
                        }

                    )

                }
            }
        }) {
            NavHost(
                navController,
                startDestination = Destinations.MapFrame.route,
                Modifier.padding(it)
            ) {
                composable(Destinations.MapFrame.route) {
                    HomeScreen()
                }
                composable(Destinations.NewsFrame.route) {
                    NewsScreen()
                }

                composable(Destinations.SettingFrame.route) {
                    SettingScreen()
                }
            }
        }
    }
}

@Preview
@Composable
fun MainBottomTabPreview() {
    MainBottomTab()
}

